public void CreateAdjGraph(int n, int e, String[] vexs, String[] edgs) {
    // 初始化顶点数组
    for (int i = 0; i < n; i++) {
        VNode v = new VNode();
        v.vex = vexs[i];
        v.firstArc = null;
        adjList[i] = v;
    }
    // 初始化边
    for (int i = 0; i < e; i++) {
        String[] edge = edgs[i].split(",");
        int v1 = -1, v2 = -1;
        for (int j = 0; j < n; j++) {
            if (adjList[j].vex.equals(edge[0])) {
                v1 = j;
            }
            if (adjList[j].vex.equals(edge[1])) {
                v2 = j;
            }
        }
        // 添加边v1 -> v2
        ArcNode newNode1 = new ArcNode();
        newNode1.adjVex = v2;
        newNode1.nextArc = adjList[v1].firstArc;
        adjList[v1].firstArc = newNode1;

        // 因为是无向图，所以还需要添加边v2 -> v1
        ArcNode newNode2 = new ArcNode();
        newNode2.adjVex = v1;
        newNode2.nextArc = adjList[v2].firstArc;
        adjList[v2].firstArc = newNode2;
    }
    // 更新顶点和边的数量
    this.n = n;
    this.e = e;
}



public int degree(int v)
{
    int degree = 0;
    ArcNode arc = adjList[v].firstArc;
    while (arc != null) {
        degree++;
        arc = arc.nextArc;
    }
    return degree;
}

public String[] getW(int v)
{
    int count = 0;
    VNode t = adjList[v];
    ArcNode p = t.firstArc;
    int arr[] = new int[n];
    while(p!=null)
    {
        arr[count] = p.adjVex;
        count++;
        p = p.nextArc;
    }
    String tmp[] = new String[count];
    for(int i = 0;i<count;i++)
    {
        tmp[i] = adjList[arr[i]].vex;
    }

    return tmp;
}



boolean[] visited = new boolean[MAXV];    // 访问标记数组
// 深度优先搜索 DFS
public void DFSAdjGraph() {
    // 重置访问标记
    for (int i = 0; i < n; i++) {
        visited[i] = false;
    }

    // 从第一个顶点（索引为0）开始DFS
    for (int i = 0; i < n; i++) {
        if (!visited[i]) {
            DFS(i);
        }
    }
}
// 深度优先搜索算法
private void DFS(int v) {
    visited[v] = true; // 标记为已访问
    System.out.print(adjList[v].vex + " "); // 输出当前顶点

    // 遍历所有邻接点
    ArcNode p = adjList[v].firstArc;
    while (p != null) {
        if (!visited[p.adjVex]) {
            DFS(p.adjVex);
        }
        p = p.nextArc;
    }
}

boolean[] visited = new boolean[MAXV];
void BFSAdjGraph()
{
    for (int i = 0; i < n; i++) {
        if (!visited[i]) {
            BFS(i);
            System.out.println();
        }
    }

}
void BFS(int v)
{
    Queue<Integer> queue = new LinkedList<>();

    queue.add(v);
    visited[v] = true;

    while (!queue.isEmpty()) {
        int cur = queue.poll();
        System.out.print(adjList[cur].vex+" ");
        ArcNode p = adjList[cur].firstArc;
        while(p!=null)
        {
            int k = p.adjVex;
            if(!visited[k])
            {
                visited[k]=true;
                queue.add(k);
            }
            p = p.nextArc;
        }
    }
}

